<!-- 新增和编辑弹窗 -->
<template>
  <a-modal
    :width='900'
    :visible='visible'
    :confirm-loading='loading'
    :title="isUpdate ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible='updateVisible'
    @ok='save'
  >
    <a-form
      layout='horizontal'
      ref='form'
      :model='form'
      :rules='rules'
      :label-col='{ md: { span: 4 }, sm: { span: 24 } }'
      :wrapper-col='{ md: { span: 20 }, sm: { span: 24 } }'
    >

      <a-form-item label='主图:' name='fileId'>
         <a-input v-model:value="form.fileId" type="hidden" />
          <a-upload
            list-type="picture-card"
            class="avatar-uploader"
            :headers="headers"
            :show-upload-list="false"
            :customRequest="uploadSeries"
          >
            <img v-if="imageUrl" :src="imageUrl" alt="avatar" class="imageUrl" style="max-width: 600px;" />
            <div v-else>
              <div class="ant-upload-text">上传主图</div>
            </div>
          </a-upload>
       </a-form-item>
      <a-form-item label='标题:' name='title'>
        <a-input v-model:value='form.title' placeholder='请输入标题' allow-clear autocomplete='off' :maxlength="20"/>
       </a-form-item>
        <a-form-item label='英文标题:' name='etitle'>
        <a-input v-model:value='form.etitle' placeholder='请输入英文标题' allow-clear autocomplete='off' :maxlength="100"/>
       </a-form-item>
      <a-form-item label='简介:' name='introduction'>
        <a-textarea v-model:value='form.introduction' placeholder='请输入简介'
         :auto-size="{ minRows: 2, maxRows: 5 }" allow-clear autocomplete='off' showCount :maxlength="200"  />
       </a-form-item>
        <a-form-item label='详情:' name='details'>
          <tinymce v-model="form.details"></tinymce>
       </a-form-item>
       
      <a-form-item label='排序:' name='sort'>
        <a-input-number v-model:value='form.sort' placeholder='请输入排序' style='width: 100%' allow-clear autocomplete='off' :maxlength="4"/>
       </a-form-item>
         <a-form-item label='状态:' name='status'>
       <a-select v-model:value="form.status" placeholder="请选择状态" allow-clear>
              <a-select-option value="1">启用</a-select-option>
              <a-select-option value="2">禁用</a-select-option>
            </a-select>
             </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { CarIndustryPlanApi } from '@/api/modular/CarIndustryPlanApi';
import Tinymce from '@/components/TinymceEditor/index';
import setting from '@/config/setting';
import { FileApi } from '@/api/FileApi';

export default {
  name: 'CarIndustryPlanEdit',
   components: {
    Tinymce
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        fileId: [{ required: true, message: '请上传主图', type: 'string', trigger: 'blur' }],
        title: [{ required: true, message: '请输入标题', type: 'string', trigger: 'blur' }],
        details: [{ required: true, message: '请输入详情', type: 'string', trigger: 'blur' }],
        introduction: [{ required: true, message: '请输入简介', type: 'string', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', type: 'number', trigger: 'blur' }],
		status: [{ required: true, message: '请选择状态', type: 'string', trigger: 'blur' }]
      },
       //图url
      imageUrl: '',
      // 上传文件时候要带header
      headers: {
        Authorization: setting.takeToken()
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
         this.form.status= String( this.form.status);
           this.imageUrl= this.handleSrc(this.form)
        this.isUpdate = true;
       
      } else {
        this.form = {};
          this.imageUrl='';
        this.isUpdate = false;
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
    }
  },
  methods: {
    /**
     * 处理image的src属性
     *
     * @author fengshuonan
     * @date 2021/5/28 15:19
     */
    handleSrc(record){
      return `/api/wx/sysFileInfo/public/preview?fileId=${record.fileId}&token=${setting.takeToken()}`;
    },
    /**
     * 保存和编辑
     *
     * @author liuxue
     * @date 2022/01/10 15:06
     */
    async save() {
      console.log(this.form);
      // 校验表单
      await this.$refs.form.validate();

      // 修改加载框为正在加载
      this.loading = true;

      let result = null;

      // 执行编辑或修改
      if (this.isUpdate) {
        result = CarIndustryPlanApi.edit(this.form);
      } else {
        result = CarIndustryPlanApi.add(this.form);
      }
      result
        .then(result => {
          // 移除加载框
          this.loading = false;

          // 提示添加成功
          this.$message.success(result.message);

          // 如果是新增，则form表单置空
          if (!this.isUpdate) {
            this.form = {};
          }

          // 关闭弹框，通过控制visible的值，传递给父组件
          this.updateVisible(false);

          // 触发父组件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author liuxue
     * @date 2022/01/10 15:06
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 上传主图
     */
    uploadSeries(file) {
      this.loading = true;
      let formData = new FormData();
      formData.append('file', file.file);
      
      let uploadResult = FileApi.commonUpload('N', formData);
      uploadResult
        .then(result => {
          // 移除加载框
          this.loading = false;
          // 提示添加成功
          this.$message.success(result.message);

          this.form.fileId = result.data.fileId;
          this.imageUrl = result.data.fileUrl;
        })
        .catch(() => {
          this.loading = false;
        });
      return false;
    }

  
  }
};
</script>
